package com.liyang.springcloud.service.impl;

import com.liyang.springcloud.dao.OrderDao;
import com.liyang.springcloud.domain.Order;
import com.liyang.springcloud.service.AccountService;
import com.liyang.springcloud.service.OrderService;
import com.liyang.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @BelongsProject: cloud-2024
 * @BelongsPackage: com.liyang.springcloud.service.impl
 * @Author: liyang
 * @CreateTime: 2024-01-10  09:50
 * @Description: 订单实现类
 * @Version: 1.0
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private AccountService accountService;

    @Resource
    private StorageService storageService;

    @Override
    @GlobalTransactional(name = "global_context", rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("----> 开始新建订单");
        orderDao.create(order);

        log.info("----> 订单微服务调用库存，做库存扣减");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("----> 订单微服务调用库存，做库存扣减end");

        log.info("----> 订单微服务调用账户，做金额扣减");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("----> 订单微服务调用账户，做金额扣减end");

        // 修改订单状态
        log.info("----> 修改订单状态");
        orderDao.update(order.getUserId(), 0);

        log.info("----> 创建订单结束 ***");
    }
}
